<template>
  <el-dialog
    v-model="dialogVisible"
    title="编辑"
    width="30%"
    :before-close="handleClose"
  >
    <div class="class-dialog">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="收货人" prop="addr_name" style="width: 100%">
          <el-input
            style="width: 100%"
            placeholder="收货人"
            v-model="ruleForm.addr_name"
          />
        </el-form-item>
        <el-form-item label="收货人电话" prop="addr_phone">
          <el-input placeholder="收货人电话" v-model="ruleForm.addr_phone" />
        </el-form-item>
        <el-form-item label="收货人地址" prop="address" style="width: 100%">
          <el-input
            placeholder="收货人地址"
            type="textarea"
            :rows="2"
            v-model="ruleForm.address"
          />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { saveAddrApi } from "@/plugins/api/customer/manage/tab4.js";
const props = defineProps({
  treeData: {
    type: Array,
    default: [],
  },
});
let ruleForm = ref({});
let rules = {
  addr_name: [{ required: true, message: "请输入收货人", trigger: "blur" }],
  addr_phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
  address: [{ required: true, message: "请输入地址", trigger: "change" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  ruleForm.value.id = row.id;
  ruleForm.value.addr_name = row.addr_name;
  ruleForm.value.addr_phone = row.addr_phone;
  ruleForm.value.address = row.address;

  dialogVisible.value = true;
}

const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      saveAddrApi(ruleForm.value).then((data) => {
        ElMessage.success("修改成功！");
        dialogVisible.value = false;
        emit("refreshList");
      });
    }
  });
}

onMounted(() => {});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
